package cn.edu.lingnan.lsqn.service;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import cn.cdahua.basic.model.Pager;
import cn.edu.lingnan.lsqn.dao.IArticleDao;
import cn.edu.lingnan.lsqn.dao.IGroupDao;
import cn.edu.lingnan.lsqn.dao.IRoleDao;
import cn.edu.lingnan.lsqn.dao.IUserDao;
import cn.edu.lingnan.lsqn.model.Article;
import cn.edu.lingnan.lsqn.model.Group;
import cn.edu.lingnan.lsqn.model.LsqnException;
import cn.edu.lingnan.lsqn.model.Role;
import cn.edu.lingnan.lsqn.model.User;

@Service("userService")
public class UserService implements IUserService {

	private IUserDao userDao;

	private IGroupDao groupDao;

	private IRoleDao roleDao;

	private IArticleDao articleDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	@Inject
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IGroupDao getGroupDao() {
		return groupDao;
	}

	@Inject
	public void setGroupDao(IGroupDao groupDao) {
		this.groupDao = groupDao;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	@Inject
	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	@Inject
	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	@Override
	public void add(User user, Integer[] g_id, Integer[] r_id) {
		if (userDao.loadByEmail(user.getEmail()) != null)
			throw new LsqnException("该用户已注册");
		// TODO 实现密码加密算法
		user.setCreateDate(new Date());
		user.setIcon("/resources/img/index/icon.png");
		user.setStatus(1);
		userDao.add(user);
		addUserGroup(user, g_id);
		addUserRole(user, r_id);
	}

	private void addUserRole(User user, Integer[] r_id) {
		if (r_id != null && r_id.length > 0) {

			for (Integer id : r_id) {
				userDao.addUserRole(user, roleDao.load(id));
			}
		}
	}

	private void addUserGroup(User user, Integer[] g_id) {
		if (g_id != null && g_id.length > 0) {
			for (Integer id : g_id) {
				userDao.addUserGroup(user, groupDao.load(id));
			}
		}
	}

	private void deleteUsergroupByUser(User user) {
		deleteUsergroupByUserId(user.getId());
	}

	private void deleteUsergroupByUserId(int id) {
		userDao.deleteUsergroupById(id);
	}

	private void deleteUserroleByUser(User user) {
		deleteUserroleByUserId(user.getId());
	}

	private void deleteUserroleByUserId(int id) {
		userDao.deleteUserroleById(id);
	}

	@Override
	public void delete(int id) {
		List<Article> articles = articleDao.listByUserId(id);
		if (articles != null && articles.size() > 0)
			throw new LsqnException("该用户有发表的文章");
		deleteUsergroupByUserId(id);
		deleteUserroleByUserId(id);
		userDao.delete(id);
	}

	@Override
	public void update(User user, Integer[] g_id, Integer[] r_id) {
		deleteUsergroupByUser(user);
		deleteUserroleByUser(user);
		update(user);
		for (Integer id : g_id) {
			userDao.addUserGroup(user, groupDao.load(id));
		}
		for (Integer id : r_id) {
			userDao.addUserRole(user, roleDao.load(id));
		}
	}

	@Override
	public void update(User user) {
		User u = userDao.load(user.getId());
		BeanUtils.copyProperties(user, u);
		userDao.update(u);
	}

	@Override
	public void updatePwd(int u_id, String oldPwd, String newPwd) {
		User u = userDao.load(u_id);
		if (u.getPassword() != oldPwd)
			throw new LsqnException("原始密码输入不正确");
		u.setPassword(newPwd);
		update(u);
	}

	@Override
	public void updateStatus(int u_id) {
		User u = userDao.load(u_id);
		if (u.getStatus() == 0)
			u.setStatus(1);
		else
			u.setStatus(0);
		update(u);
	}

	@Override
	public User load(int id) {
		return userDao.load(id);
	}

	@Override
	public User loadByName(String username) {
		return userDao.loadByName(username);
	}
	@Override
	public User loadByEmail(String email) {
		return userDao.loadByEmail(email);
	}

	@Override
	public Pager<User> find() {
		return userDao.find();
	}

	@Override
	public List<Group> listUserGroup(int id) {
		return userDao.listUserGroup(id);
	}

	@Override
	public List<Role> listUserRole(int id) {
		return userDao.listUserRole(id);
	}

	@Override
	public List<Integer> listUserRoleIds(int id) {
		return userDao.listUserRoleIds(id);
	}

	@Override
	public List<Integer> listUserGroupIds(int id) {
		return userDao.listUserGroupIds(id);
	}

	@Override
	public Pager<User> listUserByGroup(int g_id) {
		return userDao.listUserByGroup(g_id);
	}

	@Override
	public Pager<User> listUserByRole(int r_id) {
		return userDao.listUserByRole(r_id);
	}

	@Override
	public User login(User user) {
		
		User loginUser = null;
		
		if(user.getEmail()!=null && !user.getEmail().equals("")){
			loginUser = userDao.loadByEmail(user.getEmail());
		}
		if (loginUser == null){
			throw new LsqnException("您输入的账号或密码有误");
		}
		if(loginUser.getStatus()==0){	
			throw new LsqnException("该用户已被停用，请联系管理员！");
		}
		if (loginUser.getPassword().equals(user.getPassword())){
			return loginUser;
		}else{
			throw new LsqnException("您输入的账号或密码有误");
		}
	}

}
